package com.event.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.event.common.constant.KeyTag;
import com.event.common.exception.KeyParseException;
import com.event.common.model.EventCacheKey;
/*
 * @description 缓存key解析工具类(默认，可自定义实现)
 * @author Bing
 * @date 2024/10/8 13:54
 **/
public class KeyParserUtil {
    /**
     * 生成缓存key
     * @param eventCacheKey
     * @return
     */
    public static String getCacheKey(EventCacheKey eventCacheKey) {
        return getKey(false, eventCacheKey);
    }

    public static String getRegisterKey(EventCacheKey eventCacheKey){
        return getKey(true, eventCacheKey);
    }

    /**
     * 生成缓存key
     * @param isRegister
     * @param eventCacheKey
     * @return
     */
    private static String getKey(boolean isRegister, EventCacheKey eventCacheKey){
        StringBuilder sb = new StringBuilder();
        sb.append(eventCacheKey.getCacheName()).append(KeyTag.CACHENAME_TAG);
        sb.append(eventCacheKey.getNamespaceAlias()).append(KeyTag.NAMESPACE_TAG);
        sb.append(eventCacheKey.getExtra()).append(KeyTag.EXTRA_TAG);
        if(isRegister){
            if(StrUtil.isNotBlank(eventCacheKey.getBusIdName()) || ObjectUtil.isNotNull(eventCacheKey.getBusIdValue())){
                sb.append(eventCacheKey.getBusIdName()).append(KeyTag.ID_TAG);
            }
        }else{
            if(ObjectUtil.isNotNull(eventCacheKey.getBusIdValue())){
                sb.append(eventCacheKey.getBusIdValue().toString()).append(KeyTag.ID_TAG);
            }
        }
        return sb.toString();
    }

    /**
     * 解析缓存key
     * @param cacheKey
     * @return
     */
    public static EventCacheKey getCacheKey(String cacheKey) {
        if(!check(cacheKey)){
            return null;
        }
        EventCacheKey eventCacheKey = new EventCacheKey();
        int index = cacheKey.indexOf(KeyTag.CACHENAME_TAG);
        String currentKey = cacheKey;
        if(index > 0){
            String cacheName = currentKey.substring(0, index);
            currentKey = currentKey.substring(index + 1);
            eventCacheKey.setCacheName(cacheName);
        }
        index = currentKey.indexOf(KeyTag.NAMESPACE_TAG);
        if(index > 0){
            String namespaceAlias = currentKey.substring(0, index);
            currentKey = currentKey.substring(index + 1);
            eventCacheKey.setNamespaceAlias(namespaceAlias);
        }
        index = currentKey.indexOf(KeyTag.EXTRA_TAG);
        if(index > 0){
            String extra = currentKey.substring(0, index);
            currentKey = currentKey.substring(index + 1);
            eventCacheKey.setExtra(extra);
        }
        // 判断是否有ID
        if(currentKey.contains(KeyTag.ID_TAG)){
            index = currentKey.indexOf(KeyTag.ID_TAG);
            String busIdName = currentKey.substring(0, index);
            eventCacheKey.setBusIdName(busIdName);
            eventCacheKey.setBusIdValue(busIdName);
        }
        if(!eventCacheKey.check()){
            throw new KeyParseException("key format error");
        }
        return eventCacheKey;
    }

    /**
     * 检查key是否合法
     * @param cacheKey
     * @return
     */
    private static boolean check(String cacheKey){
        if(StrUtil.count(cacheKey, KeyTag.CACHENAME_TAG) != 1){
            return false;
        }
        if(StrUtil.count(cacheKey, KeyTag.NAMESPACE_TAG) != 1){
            return false;
        }
        if(StrUtil.count(cacheKey, KeyTag.EXTRA_TAG) != 1){
            return false;
        }
        return true;
    }
}
